package cn.com.jinhao.provider.business.controller;

import cn.com.jinhao.hotline.commons.controller.BaseController;
import cn.com.jinhao.hotline.commons.exception.ParamErrorException;
import cn.com.jinhao.hotline.commons.util.RestResponse;
import cn.com.jinhao.hotline.commons.vo.business.HistoryDispatchResponseVO;
import cn.com.jinhao.hotline.commons.vo.business.dispatch.DispatchCondition;
import cn.com.jinhao.provider.business.entity.Dispatch;
import cn.com.jinhao.provider.business.service.DispatchService;
import cn.com.jinhao.hotline.commons.vo.user.OrganizerRequestVO;
import cn.com.jinhao.hotline.commons.vo.user.OrganizerResponseVO;
import cn.com.jinhao.provider.business.vo.DispatchInfoRequestVO;
import cn.com.jinhao.provider.business.vo.DispatchReqeustVo;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>Description: 派单服务</p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2020/1/13
 * @since 2020-01-13
 */
@RestController
@RequestMapping("/dispatches")
@Api(tags = {"派单服务"})
public class DispatchController extends BaseController {
    @Resource
    private DispatchService dispatchService;

    /**
     * 网上派单接口
     *
     * @param dispatchInfoRequestVO 派单信息  {@link DispatchInfoRequestVO}
     * @return resp       {@link RestResponse}
     */
    @ApiOperation(value = "网上派单", notes = "网上派单")
    @PostMapping("/directDispatch")
    public RestResponse<String> addDirectDispatch(@ApiParam(name = "dispatchInfoVO", value = "网上派单信息", required = true) @Validated @RequestBody DispatchInfoRequestVO dispatchInfoRequestVO, BindingResult result) throws Exception {
        validate(result);
        dispatchService.addDirectBatch(dispatchInfoRequestVO);
        return new RestResponse<>(null);
    }

    /**
     * 派单接口
     *
     * @param dispatchReqeustVo 派单信息  {@link Dispatch}
     * @return resp       {@link RestResponse}
     */
    @ApiOperation(value = "派单", notes = "新增派单")
    @PostMapping
    public RestResponse<String> addDispatch(@ApiParam(name = "dispatch", value = "派单信息", required = true) @Validated @RequestBody DispatchReqeustVo dispatchReqeustVo, BindingResult result) throws Exception {
        validate(result);
        dispatchService.addBatch(dispatchReqeustVo);
        return new RestResponse<>(null);
    }

    @ApiOperation(value = "历史派单列表", notes = "根据事件编码查询历史派单列表")
    @GetMapping(value = "/historyByEventCode")
    public RestResponse<java.util.List<HistoryDispatchResponseVO>> history(@ApiParam(name = "eventCode", value = "事件编码", required = true) @RequestParam("eventCode") String eventCode) {
        return new RestResponse<>(dispatchService.historyDispatch(eventCode));
    }

    @ApiOperation(value = "修改主办单位", notes = "通过事件编码和部门编码修改主办单位")
    @PostMapping(value = "/changeOragnizer")
    public RestResponse<OrganizerResponseVO> changeOragnizer(@ApiParam(name = "organizerRequestVO", value = "派单信息", required = true) @RequestBody OrganizerRequestVO organizerRequestVO, BindingResult result) throws Exception {
        validate(result);
        OrganizerResponseVO organizerResponseVO = dispatchService.updateOrganizerDispatch(organizerRequestVO.getEventCode(), organizerRequestVO.getDeptCode());
        return new RestResponse<>(organizerResponseVO);
    }

    @ApiOperation(value = "查询派单信息详情", notes = "通过派单编码查询派单详细信息")
    @GetMapping
    public RestResponse<Dispatch> detail(@ApiParam(name = "code", value = "派单编码", required = true) @RequestParam String code) {
        return new RestResponse<>(dispatchService.selectByCode(code));
    }

    @ApiOperation(value = "删除派单信息", notes = "通过工单编码删除派单信息")
    @DeleteMapping
    public RestResponse<String> delete(@ApiParam(name = "code", value = "派单编码", required = true) @RequestParam String code) {
        dispatchService.deleteByCode(code);
        return new RestResponse<>(null);
    }

    @ApiOperation(value="可撤单的派单列表", notes = "可撤单的派单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptCode", value = "部门编号", required = true),
            @ApiImplicitParam(name = "isAll", value = "是否全部",dataType = "Boolean", required = true, defaultValue = "false", allowableValues = "true,false"),
            @ApiImplicitParam(name = "pageNo", value = "页数", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录条数", defaultValue = "20")
    })
    @GetMapping(value = "/cancelList")
    public RestResponse<java.util.Map<String, Object>> listCancel(@RequestParam String deptCode, @RequestParam boolean isAll, @RequestParam int pageNo, @RequestParam int pageSize) throws Exception {
        if (isAll) {
            if (pageNo <= 0) {
                throw new ParamErrorException("pageNo必须大于0");
            }
            if (pageSize <= 0) {
                throw new ParamErrorException("pageSize必须大于0");
            }
            return new RestResponse<>(dispatchService.listDispatchByCancel(deptCode, pageNo, pageSize));
        }else {
            return new RestResponse<>(dispatchService.listDispatchByCancel(deptCode, 0, 0));
        }
    }

    @ApiOperation(value = "根据条件查询派单列表")
    @GetMapping(value = "/selectByCondition")
    public RestResponse selectByCondition(@RequestBody DispatchCondition dispatchCondition, BindingResult result) throws Exception {
        validate(result);
        return new RestResponse(dispatchService.selectByCondition(dispatchCondition));
    }
}
